<template>
  <div>
    <el-drawer
      :title="title"
      :before-close="handleClose"
      :visible.sync="editOrShowSonCardDialog"
      direction="rtl"
      custom-class="add-drawer"
      ref="drawer"
      size="40%"
    >
      <div class="demo-drawer__content">
        <el-form
          :model="cardInfo"
          :rules="cardInfoRule"
          ref="cardInfoRef"
          label-position="top"
        >
          <p class="title">基础信息</p>
          <el-form-item
            label="子卡用户姓名"
            :label-width="formLabelWidth"
            prop="name"
          >
            <el-input
              v-model="cardInfo.name"
              autocomplete="off"
              :disabled="currentOperation === 'detail'"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" :label-width="formLabelWidth">
            <el-input
              v-model="cardInfo.mobilePhone"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="车牌号" :label-width="formLabelWidth">
            <el-input
              v-model="cardInfo.truckNumber"
              autocomplete="off"
              :disabled="currentOperation === 'detail'"
            ></el-input>
          </el-form-item>

          <p class="title">关联主卡</p>
          <el-form-item label="主卡号" :label-width="formLabelWidth">
            <el-input v-model="cardInfo.cardNumber" :disabled="true"></el-input>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button
            type="primary"
            @click="handleSubmit"
            v-show="currentOperation !== 'detail'"
            >确定</el-button
          >
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { updateSonCardInfoApi } from "@/api/teamCard/sonCard";
export default {
  name: "EditOrShowSonCard",
  props: {
    editOrShowSonCardDialog: {
      type: Boolean,
      default: false,
    },
    currentOperation: {
      type: String,
      default: "detail",
    },
    currentData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      cardInfo: {
        truckNumber: "",
      },
      formLabelWidth: "100px",
      cardInfoRule: {},
      cardInfoRule: {
        name: [
          { required: true, message: "请输入子卡用户姓名", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    editOrShowSonCardDialog(val) {
      if (val) {
        this.cardInfo = this.currentData;
      }
    },
  },
  computed: {
    title() {
      if (this.currentOperation === "edit") return "编辑子卡";
      return "查看子卡";
    },
  },
  methods: {
    handleClose() {
      this.$emit("update:editOrShowSonCardDialog", false);
    },
    handleSubmit() {
      this.$refs.cardInfoRef.validate((valid) => {
        if (valid) {
          console.log("submit");
          const params = {
            id: this.cardInfo.id,
            fleetCardSubForm: {
              name: this.cardInfo.name,
              truckNumber: this.cardInfo.truckNumber,
            },
          };
          updateSonCardInfoApi(params).then((res) => {
            this.msgSuccess("编辑子卡成功");
            this.handleClose();
            this.$emit("reloadData");
          });
        } else {
          console.log("error submit");
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .add-drawer {
  overflow: auto;
}
::v-deep .el-drawer__body {
  padding: 20px;
  .demo-drawer__content {
    .title {
      font-size: 17px;
    }
    .demo-drawer__footer {
      text-align: end;
    }
  }
}
</style>
